#!/usr/bin/env node

const http = require('http'),
      log = console.log,
      chalk = require('chalk'),
      fs = require('fs');

http.createServer((req,res) => {
  log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
  log(req.headers);
  log('');
  let file = __dirname + req.url;
  log(chalk.greenBright(file));
  let file_stream = fs.createReadStream(file);
  file_stream.on('error',err => {
    log(err);
    log(chalk.greenBright('出错了'));
    res.statusCode = 404;
    res.end(err.message);
  });
  file_stream.pipe(res);
}).listen(8080);
